package cmd.help;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import cmd.core.ConsoleFileManager;

public class CmdReader {
	
	private Cmd nextCmd;
	private String arg;
	
	public Cmd getNextCmd() {
		return nextCmd;
	}

	public String getArg() {
		return arg;
	}

	private BufferedReader toReadCmd = new BufferedReader(new InputStreamReader(System.in));
	
	private Cmd cmdParser(String toParse){
		if(toParse.equalsIgnoreCase(Cmd.CD.toString()) || toParse.equals(ConsoleFileManager.PARENT))
			return Cmd.CD;
		if(toParse.equalsIgnoreCase(Cmd.LS.toString()))
			return Cmd.LS;
		if(toParse.equalsIgnoreCase(Cmd.TOUCH.toString()))
			return Cmd.TOUCH;
		if(toParse.equalsIgnoreCase(Cmd.MKDIR.toString()))
			return Cmd.MKDIR;
		if(toParse.equalsIgnoreCase(Cmd.EXIT.toString()))
			return Cmd.EXIT;
		return Cmd.HELP;
	}
	
	public void readNextCmd(){
		try {
			arg = null;
			String userInput = toReadCmd.readLine();
			setCmdAndArg(userInput);
		} catch (IOException e) {
			nextCmd = Cmd.EXIT;
		}
	}

	private void setCmdAndArg(String userInput) {
		String[] cmdAndArgs = userInput.split(" ");
		String resStr = "";
		for(int i = 1; i < cmdAndArgs.length;++i){
			resStr += cmdAndArgs[i];
			if(i != cmdAndArgs.length -1)
				resStr +=" ";
		}
		if(cmdAndArgs.length == 0){
			nextCmd = Cmd.HELP;
			return;
		}
		if(cmdAndArgs.length >= 2){
			arg = resStr.replace('\\', '/');
		}
		if(cmdAndArgs[0].equals(ConsoleFileManager.PARENT))
			arg = cmdAndArgs[0];
		nextCmd = cmdParser(cmdAndArgs[0]);
	}
}
